<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>计算子网</title>
</head>

<body>
  <form>
    <label for="ip">IP 地址：</label>
    <input type="text" id="ip" name="ip" value="152.178.132.95"><br>
    <label for="maskBits">子网掩码位数：</label>
    <input type="number" value="24" id="maskBits" name="maskBits"><br>
    <label for="subnetBits">子网位数：</label>
    <input type="number" value="3" id="subnetBits" name="subnetBits"><br>
    <button type="button" onclick="calculateSubnet()">计算</button>
  </form>

  <pre id="output"></pre>

  <script>
    // 将二进制转换成点分十进制表示
    function binaryToIp(binary) {
      return parseInt(binary, 2).toString(10).match(/\d{1,3}/g).join('.');
    }

    function calculateSubnet() {
      // 获取输入值
      let ip = document.getElementById('ip').value;
      let maskBits = parseInt(document.getElementById('maskBits').value);
      let subnetBits = parseInt(document.getElementById('subnetBits').value);

      // 执行计算
      let output = '';
      output += '计算过程：\n';

      // 将 IP 地址转换为二进制和十六进制
      output += '将 IP 地址转换为二进制和十六进制：\n';
      let binary = ipToBinary(ip);
      let hex = ipToHex(ip);
      output += `  ${ip} => ${binary} => ${hex}\n\n`;

      output += '计算子网掩码：\n';
      let mask = (1 << maskBits) - 1;
      console.log("mask");
      console.log(mask);

      output += `  子网掩码位数：${maskBits}\n`;
      let maskBinStr=
      mask.toString(2).padStart(32, '0')
      console.log("maskBinStr");
      console.log(maskBinStr);
      output +=`  子网掩码：${maskBinStr}\n`
      // output +=
      //   `  子网掩码：${binaryToIp(mask.toString(2).padStart(32, '0'))} / ${binaryToIp(mask.toString(2).padStart(32, '0'), true)}\n\n`;

      // 计算子网id 
      output += '计算子网 ID 和主机数：\n';
      let subnetInfo = subnet(ip, maskBits, subnetBits);
      output += `  子网掩码位数：${maskBits}\n`;
      output += `  子网位数：${subnetBits}\n`;
      output += `  子网 ID：${subnetInfo.subnet}\n`;
      output += `  主机数：${subnetInfo.hostsPerSubnet}\n\n`;

      output += '计算子网地址范围：\n';
      output +=
        `  第一个 IP 地址：${subnetInfo.firstIp} / ${binaryToIp(subnetInfo.firstIp.toString(2).padStart(32, '0'), true)}\n`;
      output +=
        `  最后一个 IP 地址：${subnetInfo.lastIp} / ${binaryToIp(subnetInfo.lastIp.toString(2).padStart(32, '0'), true)}\n\n`;

      // 显示输出结果
      document.getElementById('output').textContent = output;
    }

    function ipToHex(ip) {
      let parts = ip.split('.');
      let hex = parts.map(part => parseInt(part).toString(16).padStart(2, '0')).join('.');
      return hex;
    }

    // function binaryToIp(binary, isHex = false) {
    //   let base = isHex ? 16 : 10;
    //   return parseInt(binary, 2).toString(base).match(/\d{1,3}/g).join('.');
    // }

    // 将二进制转换成点分十进制表示
    //   function binaryToIp(binary) {
    //     return parseInt(binary, 2).toString(10).match(/\d{1,3}/g).join('.');
    //   }

    // function calculateSubnet() {
    //   // 获取输入值
    //   let ip = document.getElementById('ip').value;
    //   let maskBits = parseInt(document.getElementById('maskBits').value);
    //   let subnetBits = parseInt(document.getElementById('subnetBits').value);

    //   // 执行计算
    //   let output = '';
    //   output += '计算过程：\n';

    //   output += '将 IP 地址转换为二进制：\n';
    //   let binary = ipToBinary(ip);
    //   output += `  ${ip} => ${binary}\n\n`;

    //   output += '计算子网掩码：\n';
    //   let mask = (1 << maskBits) - 1;
    //   output += `  子网掩码位数：${maskBits}\n`;
    //   output += `  子网掩码：${binaryToIp(mask.toString(2).padStart(32, '0'))}\n\n`;

    //   output += '计算子网 ID 和主机数：\n';
    //   let subnetInfo = subnet(ip, maskBits, subnetBits);
    //   output += `  子网掩码位数：${maskBits}\n`;
    //   output += `  子网位数：${subnetBits}\n`;
    //   output += `  子网 ID：${subnetInfo.subnet}\n`;
    //   output += `  主机数：${subnetInfo.hostsPerSubnet}\n\n`;

    //   output += '计算子网地址范围：\n';
    //   output += `  第一个 IP 地址：${subnetInfo.firstIp}\n`;
    //   output += `  最后一个 IP 地址：${subnetInfo.lastIp}\n\n`;

    //   // 显示输出结果
    //   document.getElementById('output').textContent = output;
    // }

    function ipToBinary(ip) {
      // 将 IP 地址拆分成四个数组
      let parts = ip.split('.');
      // 将每个数组转换成 8 位二进制数，并拼接起来
      let binary = parts.map(part => parseInt(part).toString(2).padStart(8, '0')).join('.');
      return binary;
    }

    // function subnet(ip, maskBits, subnetBits) {
    //   // 计算子网掩码
    //   let mask = (1 << maskBits) - 1;
    //   // 计算子网 ID
    //   let subnetId = mask << (32 - maskBits * subnetBits);
    //   // 计算每个子网的主机数
    //   let hostsPerSubnet = 1 << subnetBits;
    //   // 计算子网地址范围
    //   let ipBinary = parseInt(ip.split('.').map(part => 
    //   parseInt(part).toString(2).padStart(8, '0')).join(''), 2);
    //   let firstIp = (ipBinary & subnetId) >>> 0;
    //   let lastIp = (firstIp + hostsPerSubnet - 1) >>> 0;

    //   let  bin=firstIp.toString(2).padStart(32, '0')
    //   console.log("bin");
    //   console.log(bin);

    //   console.log("lastIp");
    //   console.log(lastIp);

    //   let firstIpStr = binaryToIp(firstIp.toString(2).padStart(32, '0'));
    //   console.log("firstIpStr");
    //   console.log(firstIpStr);

    //   console.log("maskBits");
    //   console.log(maskBits);
    //   console.log("subnetBits");
    //   console.log(subnetBits);


    //   let lastIpStr = binaryToIp(lastIp.toString(2).padStart(32, '0'));
    //   // 返回子网地址范围和主机数
    //   return {
    //     subnet: `${firstIpStr}/${maskBits + subnetBits}`,
    //     firstIp: firstIpStr,
    //     lastIp: lastIpStr,
    //     hostsPerSubnet: hostsPerSubnet - 2 // 去掉网络地址和广播地址
    //   };
    // }

    function toBinStr(subnetId){
      let subnetIdBinStr =  subnetId.toString(2).padStart(32, '0')

      return subnetIdBinStr
    }
    function subnet(ip, maskBits, subnetBits) {
  // 计算子网掩码
  let mask = (1 << maskBits) - 1;
  // 计算子网 ID
  let subnetId = mask << (32 - maskBits - subnetBits);
  console.log("subnetId 算子网 ID");
  console.log(subnetId);

  let subnetIdBinStr =  toBinStr(subnetId)
  // let subnetIdBinStr =  subnetId.toString(2).padStart(32, '0')
  console.log("subnetId  subnetIdBinStr 算子网 ID");
  console.log(subnetIdBinStr);
  // 计算每个子网的主机数
  let hostsPerSubnet = 1 << subnetBits;
  // 计算子网地址范围
  let ipBinary = parseInt(ip.split('.').map(part => 
    parseInt(part).toString(2).padStart(8, '0')).join(''), 2);
  let firstIp = ipBinary & subnetId;
  let lastIp = (firstIp + hostsPerSubnet - 1) >>> 0;

  let bin = firstIp.toString(2).padStart(32, '0');
  console.log("bin");
  console.log(bin);

  console.log("lastIp");
  console.log(lastIp);

  let firstIpStr = binaryToIp(firstIp.toString(2).padStart(32, '0'));
  console.log("firstIpStr");
  console.log(firstIpStr);

  console.log("maskBits");
  console.log(maskBits);
  console.log("subnetBits");
  console.log(subnetBits);

  let lastIpStr = binaryToIp(lastIp.toString(2).padStart(32, '0'));
  // 返回子网地址范围和主机数
  return {
    subnet: `${firstIpStr}/${maskBits + subnetBits}`,
    firstIp: firstIpStr,
    lastIp: lastIpStr,
    hostsPerSubnet: hostsPerSubnet - 2 // 去掉网络地址和广播地址
  };
}

  </script>
</body>

</html>